package down.sallai.ml.services;

import down.sallai.ml.bean.FileInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Arrays;

@Slf4j
@Service
public class FileMangerImpl implements FileManger {
    @Value(value = "${mydown.fileLocal}")
    private String filePath;

    @Override
    public Boolean deleteAllFile(String fileLocal) {
        File file = new File(fileLocal);
        if(!file.isDirectory()) return false;
        File[] files = file.listFiles();
        for(File f : files){
            if(f.isDirectory()) {
                try {
                    deleteAllFile(f.getCanonicalPath());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{

                f.delete();
            }

        }
        return true;
    }


    @Override
    public ArrayList<FileInfo> getAllFile(String fileLocal) {
        BasicFileAttributes bAttributes = null;

        File file = new File(fileLocal);
        if(!file.isDirectory()) return null;
        File[] files = file.listFiles();
        ArrayList<FileInfo> f1 = new ArrayList<FileInfo>();



        for(File f : files){
            try {
                bAttributes = Files.readAttributes(f.toPath(),
                        BasicFileAttributes.class);
            } catch (IOException e) {
                e.printStackTrace();
            }

// 修改时间
            String changeTime = bAttributes.lastModifiedTime().toString();
            System.out.println("文件创建时间"+changeTime);
            changeTime = changeTime.replaceAll("T", " ");
            changeTime = changeTime.substring(0,19)+"+8";

            if(f.length()>=(1024*1024)){

                f1.add(new FileInfo(f.getName(),changeTime,f.length()/(1024*1024)+"mb"));
            }else{
                f1.add(new FileInfo(f.getName(),changeTime,f.length()/(1024)+"kb"));
            }


        }
        System.out.println(f1.size());
        return f1;
    }

    @Override
    public Boolean delByName(String name,String fileLocal) {
        File file = new File(fileLocal + name);
        boolean delete = file.delete();
        return delete;
    }

    @Override
    public void clearAllFile(String filePath) {
        log.info("clear all fiel path is "+filePath);
        if( null == filePath || "".equals(filePath)) {
            log.error("clear file path is null or empty");
            return;
        }
        File file = new File(filePath);
        if(!file.isDirectory()) {
            log.error("clear file path is not a dir");
            return;
        }
        Arrays.stream(file.listFiles()).forEach(e->{
            if (e.isFile()) e.delete();
        });
        log.info("clear all file is end");
    }

    @Override
    public void clearAllFile() {
        clearAllFile(filePath);
    }


}
